package com.yurendao.shop.user.service.impl;

import com.yurendao.shop.user.dao.UserMapper;
import com.yurendao.shop.user.domain.User;
import com.yurendao.shop.user.domain.UserExample;
import com.yurendao.shop.user.service.UserService;
import com.yurendao.shop.user.service.exception.UserException;
import com.yurendao.shop.utils.CommonUtils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * Created by kong on 15/10/2.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.REQUIRES_NEW,readOnly = false,isolation = Isolation.DEFAULT)
    @Override
    public void insertUser(User user) throws UserException {
        user.setUid(CommonUtils.uuid());
        user.setActivitycode(CommonUtils.uuid()+CommonUtils.uuid()+"av");
        userMapper.insert(user);
    }

    @Override
    public void ajaxUname(String uname) throws UserException {
        UserExample example = new UserExample();
        example.createCriteria().andUnameEqualTo(uname);
        List<User> users = userMapper.selectByExample(example);
        if(users.size()!=0)
            throw new UserException("username was already exists");
    }

    @Override
    public User login(User user) throws UserException {

        UserValidate.validate(user,"login");
        //加密
//        Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
//        String encodepass = md5PasswordEncoder.encodePassword(user.getPassword(),null);

        String encodepass = user.getPassword();
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUnameEqualTo(user.getUname()).andPasswordEqualTo(encodepass);
        List<User> loginuser = userMapper.selectByExample(userExample);
        if(loginuser.size()==1)
            return loginuser.get(0);
        
        userExample.clear();
        userExample.createCriteria().andEmailEqualTo(user.getEmail()).andPasswordEqualTo(encodepass);
        loginuser = userMapper.selectByExample(userExample);
        if(loginuser.size()==1)
            return loginuser.get(0);

        userExample.clear();
        userExample.createCriteria().andPhoneEqualTo(user.getPhone()).andPasswordEqualTo(encodepass);
        loginuser = userMapper.selectByExample(userExample);
        if(loginuser.size()==1)
            return loginuser.get(0);
        return null;
    }

    @Override
    public void telephoneRegist(String telephone) throws UserException {
        
    }

    @Override
    public void modifyDetail(User userDetail) throws UserException {
        UserValidate.validate(userDetail,"modify");
        userMapper.updateByPrimaryKey(userDetail);
    }

    @Override
    public String getNameByID(String uid) throws UserException {
        return userMapper.getNameById(uid);
    }


}
